package com.redteam.xomtro;

import android.content.Intent;
import android.graphics.Typeface;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.view.ContextMenu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.redteam.xomtro.fragment.HomeFragment;
import com.redteam.xomtro.fragment.MainMenuFragment;
import com.redteam.xomtro.fragment.SecondMenuFragment;
import com.slidingmenu.lib.SlidingMenu;
import com.slidingmenu.lib.app.SlidingFragmentActivity;


public class MainActivity extends SlidingFragmentActivity implements View.OnClickListener {

    private Fragment mContent;
    private Fragment mMenu;
    private Fragment mSecondMenu;
    private FragmentManager fragmentManager;
    private SlidingMenu sm;
    private View selectedTab;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getSupportActionBar().setCustomView(R.layout.action_bar_main);
        getSupportActionBar().setDisplayShowHomeEnabled(false);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setDisplayShowCustomEnabled(true);
        TextView tvLogo = (TextView) findViewById(R.id.tvLogo);
        tvLogo.setText(getString(R.string.app_name).toLowerCase());
        tvLogo.setTypeface(Typeface.createFromAsset(getAssets(), "fonts/solgas.ttf"));

        // set the Behind View
        setBehindContentView(R.layout.menu_frame);
        if (savedInstanceState == null) {
            fragmentManager = this.getSupportFragmentManager();
            mMenu = new MainMenuFragment().newInstance(getSlidingMenu(),tvLogo);
            mSecondMenu = new SecondMenuFragment();
            mContent = new HomeFragment(this);
        } else {
            mMenu = getSupportFragmentManager().getFragment(savedInstanceState, "mMenu");
            mSecondMenu = getSupportFragmentManager().getFragment(savedInstanceState, "mSecondMenu");
            mContent = getSupportFragmentManager().getFragment(savedInstanceState, "mContent");
        }
        fragmentManager.beginTransaction().replace(R.id.menu_frame, mMenu).commit();

        // customize the SlidingMenu
        sm = this.getSlidingMenu();
        sm.setShadowWidthRes(R.dimen.shadow_width);
        sm.setShadowDrawable(R.drawable.shadow);
        sm.setBehindOffsetRes(R.dimen.slidingmenu_offset);
        sm.setFadeDegree(0.35f);
        sm.setMode(SlidingMenu.LEFT_RIGHT);
        sm.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);

        setContentView(R.layout.content_frame);
        fragmentManager.beginTransaction().replace(R.id.content_frame, mContent).commit();

        sm.setSecondaryMenu(R.layout.menu_frame_two);
        sm.setSecondaryShadowDrawable(R.drawable.shadowright);
        fragmentManager.beginTransaction().replace(R.id.menu_frame_two, mSecondMenu).commit();

        findViewById(R.id.btnMenu).setOnClickListener(this);
        findViewById(R.id.btnMap).setOnClickListener(this);
        int screenW = WellcomeActivity.widthScreen / 5;
        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(screenW, ViewGroup.LayoutParams.WRAP_CONTENT);
        selectedTab = findViewById(R.id.actionTab_hotRoom);
        selectedTab.setLayoutParams(layoutParams);
        selectedTab.setOnClickListener(this);
        findViewById(R.id.actionTab_map).setLayoutParams(layoutParams);
        findViewById(R.id.actionTab_map).setOnClickListener(this);
//        findViewById(R.id.actionTab_add).setLayoutParams(new LinearLayout.LayoutParams(screenW, (int) Support.dpToPixel(40, this)));
//        findViewById(R.id.actionTab_add).setOnClickListener(this);
        View tabNews = findViewById(R.id.actionTab_news);
        tabNews.setLayoutParams(layoutParams);
        tabNews.setOnClickListener(this);
        tabNews.setOnCreateContextMenuListener(this);
        findViewById(R.id.actionTab_info).setLayoutParams(layoutParams);
        findViewById(R.id.actionTab_info).setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        if (selectedTab == view)
            return;
        switch (view.getId()) {
            case R.id.btnMenu:
                sm.showMenu(true);
                break;
            case R.id.btnMap:
                startActivity(new Intent(this, RoomMapActivity.class));
                break;
            case R.id.actionTab_hotRoom:
                setTabMode(view, R.drawable.ic_action_all_on);
                unSelectTab(selectedTab);
                selectedTab = view;
                //TODO load add event
                break;
            case R.id.actionTab_map:
                setTabMode(view, R.drawable.ic_action_traffic_on);
                unSelectTab(selectedTab);
                selectedTab = view;
                //TODO load event with traffic
                break;
            case R.id.actionTab_news:
                setTabMode(view, R.drawable.ic_action_date_on);
                unSelectTab(selectedTab);
                selectedTab = view;
                //TODO load event with date
                break;
            case R.id.actionTab_info:
                sm.showSecondaryMenu(true);
                break;
        }
    }

    @Override
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
        super.onCreateContextMenu(menu, v, menuInfo);
        MenuItem mnu1 = menu.add(0, 0, 0, getString(R.string.txt_menu_today_item));
        {
            mnu1.setIcon(R.drawable.ic_menu_today);
        }
        MenuItem mnu2 = menu.add(0, 1, 1, getString(R.string.txt_menu_tomorrow_item));
        {
            mnu2.setIcon(R.drawable.ic_menu_tomorrow);
        }
        MenuItem mnu3 = menu.add(0, 2, 2, getString(R.string.txt_menu_weekend_item));
        {
            mnu3.setIcon(R.drawable.ic_menu_weekend);
        }
        MenuItem mnu4 = menu.add(0, 3, 3, getString(R.string.txt_menu_date_group));
        {
            mnu4.setIcon(R.drawable.ic_menu_date);
        }
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        getSupportFragmentManager().putFragment(outState, "mMenu", mMenu);
        getSupportFragmentManager().putFragment(outState, "mSecondMenu", mSecondMenu);
        getSupportFragmentManager().putFragment(outState, "mContent", mContent);
    }

    private void setTabMode(View v, int img) {
        ViewGroup viewGroup = (ViewGroup) v;
        ImageView imageView = (ImageView) viewGroup.getChildAt(0);
        if (imageView != null)
            imageView.setImageResource(img);
    }

    private void unSelectTab(View v) {
        switch (v.getId()) {
            case R.id.actionTab_hotRoom:
                setTabMode(v, R.drawable.ic_action_all_off);
                break;
            case R.id.actionTab_map:
                setTabMode(v, R.drawable.ic_action_traffic_off);
                break;
            case R.id.actionTab_news:
                setTabMode(v, R.drawable.ic_action_date_off);
                break;
        }
    }

    @Override
    public void onLowMemory() {
        System.gc();
    }
}
